from datetime import datetime
from app.extensions import db

from datetime import datetime

class ReviewPlan(db.Model):
    """复习计划表模型"""
    __tablename__ = 'review_plan'
    plan_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='计划唯一标识')
    initial_date = db.Column(db.Date, nullable=False, comment='初始学习日期')
    next_review_date = db.Column(db.Date, nullable=False, comment='下次复习日期')
    interval_days = db.Column(db.SmallInteger, nullable=False, comment='间隔天数')
    ebbinghaus_stage = db.Column(db.SmallInteger, nullable=False, comment='艾宾浩斯阶段')
    
    # 逻辑外键字段
    student_id = db.Column(db.Integer, db.ForeignKey('student.student_id'), 
                          nullable=False, comment='学生ID')
    content_id = db.Column(db.Integer, db.ForeignKey('content.content_id'), 
                           nullable=False, comment='内容ID')

    def to_dict(self, date_format='%Y-%m-%d'):
        """
        将模型实例转换为字典，并格式化日期字段。
        
        :param date_format: 日期格式化字符串，默认为 'YYYY-MM-DD'
        :return: 包含格式化日期的字典
        """
        return {
            'plan_id': self.plan_id,
            'initial_date': self.initial_date.strftime(date_format),  # 格式化初始学习日期
            'next_review_date': self.next_review_date.strftime(date_format),  # 格式化下次复习日期
            'interval_days': self.interval_days,
            'ebbinghaus_stage': self.ebbinghaus_stage,
            'student_id': self.student_id,
            'content_id': self.content_id
        }

    def __repr__(self):
        return f'<ReviewPlan {self.plan_id} (Stage {self.ebbinghaus_stage})>'